
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.contrib.gis.geoip.base &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-gis-geoip-base">
            
  <h1>Source code for django.contrib.gis.geoip.base</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">ctypes</span> <span class="k">import</span> <span class="n">c_char_p</span>

<span class="kn">from</span> <span class="nn">django.contrib.gis.geoip.libgeoip</span> <span class="k">import</span> <span class="n">GEOIP_SETTINGS</span>
<span class="kn">from</span> <span class="nn">django.contrib.gis.geoip.prototypes</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">GeoIP_country_code_by_addr</span><span class="p">,</span> <span class="n">GeoIP_country_code_by_name</span><span class="p">,</span>
    <span class="n">GeoIP_country_name_by_addr</span><span class="p">,</span> <span class="n">GeoIP_country_name_by_name</span><span class="p">,</span>
    <span class="n">GeoIP_database_info</span><span class="p">,</span> <span class="n">GeoIP_delete</span><span class="p">,</span> <span class="n">GeoIP_lib_version</span><span class="p">,</span> <span class="n">GeoIP_open</span><span class="p">,</span>
    <span class="n">GeoIP_record_by_addr</span><span class="p">,</span> <span class="n">GeoIP_record_by_name</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.core.validators</span> <span class="k">import</span> <span class="n">ipv4_re</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="k">import</span> <span class="n">RemovedInDjango20Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_bytes</span><span class="p">,</span> <span class="n">force_text</span>

<span class="c1"># Regular expressions for recognizing the GeoIP free database editions.</span>
<span class="n">free_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^GEO-\d</span><span class="si">{3}</span><span class="s1">FREE&#39;</span><span class="p">)</span>
<span class="n">lite_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^GEO-\d</span><span class="si">{3}</span><span class="s1">LITE&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">GeoIPException</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>


<div class="viewcode-block" id="GeoIP"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP">[docs]</a><span class="k">class</span> <span class="nc">GeoIP</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="c1"># The flags for GeoIP memory caching.</span>
    <span class="c1"># GEOIP_STANDARD - read database from filesystem, uses least memory.</span>
    <span class="c1">#</span>
    <span class="c1"># GEOIP_MEMORY_CACHE - load database into memory, faster performance</span>
    <span class="c1">#        but uses more memory</span>
    <span class="c1">#</span>
    <span class="c1"># GEOIP_CHECK_CACHE - check for updated database.  If database has been</span>
    <span class="c1">#        updated, reload filehandle and/or memory cache.  This option</span>
    <span class="c1">#        is not thread safe.</span>
    <span class="c1">#</span>
    <span class="c1"># GEOIP_INDEX_CACHE - just cache the most frequently accessed index</span>
    <span class="c1">#        portion of the database, resulting in faster lookups than</span>
    <span class="c1">#        GEOIP_STANDARD, but less memory usage than GEOIP_MEMORY_CACHE -</span>
    <span class="c1">#        useful for larger databases such as GeoIP Organization and</span>
    <span class="c1">#        GeoIP City.  Note, for GeoIP Country, Region and Netspeed</span>
    <span class="c1">#        databases, GEOIP_INDEX_CACHE is equivalent to GEOIP_MEMORY_CACHE</span>
    <span class="c1">#</span>
    <span class="c1"># GEOIP_MMAP_CACHE - load database into mmap shared memory ( not available</span>
    <span class="c1">#       on Windows).</span>
    <span class="n">GEOIP_STANDARD</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">GEOIP_MEMORY_CACHE</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">GEOIP_CHECK_CACHE</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">GEOIP_INDEX_CACHE</span> <span class="o">=</span> <span class="mi">4</span>
    <span class="n">GEOIP_MMAP_CACHE</span> <span class="o">=</span> <span class="mi">8</span>
    <span class="n">cache_options</span> <span class="o">=</span> <span class="p">{</span><span class="n">opt</span><span class="p">:</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">opt</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">8</span><span class="p">)}</span>

    <span class="c1"># Paths to the city &amp; country binary databases.</span>
    <span class="n">_city_file</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">_country_file</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="c1"># Initially, pointers to GeoIP file references are NULL.</span>
    <span class="n">_city</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">_country</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cache</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">country</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">city</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes the GeoIP object, no parameters are required to use default</span>
<span class="sd">        settings.  Keyword arguments may be passed in to customize the locations</span>
<span class="sd">        of the GeoIP data sets.</span>

<span class="sd">        * path: Base directory to where GeoIP data is located or the full path</span>
<span class="sd">            to where the city or country data files (*.dat) are located.</span>
<span class="sd">            Assumes that both the city and country data sets are located in</span>
<span class="sd">            this directory; overrides the GEOIP_PATH settings attribute.</span>

<span class="sd">        * cache: The cache settings when opening up the GeoIP datasets,</span>
<span class="sd">            and may be an integer in (0, 1, 2, 4, 8) corresponding to</span>
<span class="sd">            the GEOIP_STANDARD, GEOIP_MEMORY_CACHE, GEOIP_CHECK_CACHE,</span>
<span class="sd">            GEOIP_INDEX_CACHE, and GEOIP_MMAP_CACHE, `GeoIPOptions` C API</span>
<span class="sd">            settings,  respectively.  Defaults to 0, meaning that the data is read</span>
<span class="sd">            from the disk.</span>

<span class="sd">        * country: The name of the GeoIP country data file.  Defaults to</span>
<span class="sd">            &#39;GeoIP.dat&#39;; overrides the GEOIP_COUNTRY settings attribute.</span>

<span class="sd">        * city: The name of the GeoIP city data file.  Defaults to</span>
<span class="sd">            &#39;GeoLiteCity.dat&#39;; overrides the GEOIP_CITY settings attribute.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
            <span class="s2">&quot;django.contrib.gis.geoip is deprecated in favor of &quot;</span>
            <span class="s2">&quot;django.contrib.gis.geoip2 and the MaxMind GeoLite2 database &quot;</span>
            <span class="s2">&quot;format.&quot;</span><span class="p">,</span> <span class="n">RemovedInDjango20Warning</span><span class="p">,</span> <span class="mi">2</span>
        <span class="p">)</span>

        <span class="c1"># Checking the given cache option.</span>
        <span class="k">if</span> <span class="n">cache</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache_options</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span> <span class="o">=</span> <span class="n">cache</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;Invalid GeoIP caching option: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">cache</span><span class="p">)</span>

        <span class="c1"># Getting the GeoIP data path.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">GEOIP_SETTINGS</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GEOIP_PATH&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;GeoIP path must be provided via parameter or the GEOIP_PATH setting.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Invalid path type: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="c1"># Constructing the GeoIP database filenames using the settings</span>
            <span class="c1"># dictionary.  If the database files for the GeoLite country</span>
            <span class="c1"># and/or city datasets exist, then try and open them.</span>
            <span class="n">country_db</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">country</span> <span class="ow">or</span> <span class="n">GEOIP_SETTINGS</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GEOIP_COUNTRY&#39;</span><span class="p">,</span> <span class="s1">&#39;GeoIP.dat&#39;</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">country_db</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_country</span> <span class="o">=</span> <span class="n">GeoIP_open</span><span class="p">(</span><span class="n">force_bytes</span><span class="p">(</span><span class="n">country_db</span><span class="p">),</span> <span class="n">cache</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_country_file</span> <span class="o">=</span> <span class="n">country_db</span>

            <span class="n">city_db</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">city</span> <span class="ow">or</span> <span class="n">GEOIP_SETTINGS</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;GEOIP_CITY&#39;</span><span class="p">,</span> <span class="s1">&#39;GeoLiteCity.dat&#39;</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">city_db</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_city</span> <span class="o">=</span> <span class="n">GeoIP_open</span><span class="p">(</span><span class="n">force_bytes</span><span class="p">(</span><span class="n">city_db</span><span class="p">),</span> <span class="n">cache</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_city_file</span> <span class="o">=</span> <span class="n">city_db</span>
        <span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="c1"># Otherwise, some detective work will be needed to figure</span>
            <span class="c1"># out whether the given database path is for the GeoIP country</span>
            <span class="c1"># or city databases.</span>
            <span class="n">ptr</span> <span class="o">=</span> <span class="n">GeoIP_open</span><span class="p">(</span><span class="n">force_bytes</span><span class="p">(</span><span class="n">path</span><span class="p">),</span> <span class="n">cache</span><span class="p">)</span>
            <span class="n">info</span> <span class="o">=</span> <span class="n">GeoIP_database_info</span><span class="p">(</span><span class="n">ptr</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">lite_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">info</span><span class="p">):</span>
                <span class="c1"># GeoLite City database detected.</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_city</span> <span class="o">=</span> <span class="n">ptr</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_city_file</span> <span class="o">=</span> <span class="n">path</span>
            <span class="k">elif</span> <span class="n">free_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">info</span><span class="p">):</span>
                <span class="c1"># GeoIP Country database detected.</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_country</span> <span class="o">=</span> <span class="n">ptr</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_country_file</span> <span class="o">=</span> <span class="n">path</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;Unable to recognize database edition: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">info</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;GeoIP path must be a valid file or directory.&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Cleaning any GeoIP file handles lying around.</span>
        <span class="k">if</span> <span class="n">GeoIP_delete</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">:</span>
            <span class="n">GeoIP_delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">:</span>
            <span class="n">GeoIP_delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">version</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">GeoIP_lib_version</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">version</span> <span class="o">+=</span> <span class="s1">&#39; [v</span><span class="si">%s</span><span class="s1">]&#39;</span> <span class="o">%</span> <span class="n">force_text</span><span class="p">(</span><span class="n">GeoIP_lib_version</span><span class="p">())</span>
        <span class="k">return</span> <span class="s1">&#39;&lt;</span><span class="si">%(cls)s%(version)s</span><span class="s1"> _country_file=&quot;</span><span class="si">%(country)s</span><span class="s1">&quot;, _city_file=&quot;</span><span class="si">%(city)s</span><span class="s1">&quot;&gt;&#39;</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s1">&#39;cls&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s1">&#39;version&#39;</span><span class="p">:</span> <span class="n">version</span><span class="p">,</span>
            <span class="s1">&#39;country&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country_file</span><span class="p">,</span>
            <span class="s1">&#39;city&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city_file</span><span class="p">,</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_check_query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">country</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">city</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">city_or_country</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="s2">&quot;Helper routine for checking the query and database availability.&quot;</span>
        <span class="c1"># Making sure a string was passed in for the query.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;GeoIP query must be a string, not type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">query</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>

        <span class="c1"># Extra checks for the existence of country and city databases.</span>
        <span class="k">if</span> <span class="n">city_or_country</span> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;Invalid GeoIP country and city data files.&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">country</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;Invalid GeoIP country data file: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country_file</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">city</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">GeoIPException</span><span class="p">(</span><span class="s1">&#39;Invalid GeoIP city data file: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city_file</span><span class="p">)</span>

        <span class="c1"># Return the query string back to the caller. GeoIP only takes bytestrings.</span>
        <span class="k">return</span> <span class="n">force_bytes</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>

<div class="viewcode-block" id="GeoIP.city"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.city">[docs]</a>    <span class="k">def</span> <span class="nf">city</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a dictionary of city information for the given IP address or</span>
<span class="sd">        Fully Qualified Domain Name (FQDN).  Some information in the dictionary</span>
<span class="sd">        may be undefined (None).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">enc_query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_check_query</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">city</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ipv4_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">query</span><span class="p">):</span>
            <span class="c1"># If an IP address was passed in</span>
            <span class="k">return</span> <span class="n">GeoIP_record_by_addr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">,</span> <span class="n">c_char_p</span><span class="p">(</span><span class="n">enc_query</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># If a FQDN was passed in.</span>
            <span class="k">return</span> <span class="n">GeoIP_record_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">,</span> <span class="n">c_char_p</span><span class="p">(</span><span class="n">enc_query</span><span class="p">))</span></div>

<div class="viewcode-block" id="GeoIP.country_code"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.country_code">[docs]</a>    <span class="k">def</span> <span class="nf">country_code</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="s2">&quot;Returns the country code for the given IP Address or FQDN.&quot;</span>
        <span class="n">enc_query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_check_query</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">city_or_country</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">ipv4_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">query</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">GeoIP_country_code_by_addr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">,</span> <span class="n">enc_query</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">GeoIP_country_code_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">,</span> <span class="n">enc_query</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">city</span><span class="p">(</span><span class="n">query</span><span class="p">)[</span><span class="s1">&#39;country_code&#39;</span><span class="p">]</span></div>

<div class="viewcode-block" id="GeoIP.country_name"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.country_name">[docs]</a>    <span class="k">def</span> <span class="nf">country_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="s2">&quot;Returns the country name for the given IP Address or FQDN.&quot;</span>
        <span class="n">enc_query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_check_query</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">city_or_country</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">ipv4_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">query</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">GeoIP_country_name_by_addr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">,</span> <span class="n">enc_query</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">GeoIP_country_name_by_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">,</span> <span class="n">enc_query</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">city</span><span class="p">(</span><span class="n">query</span><span class="p">)[</span><span class="s1">&#39;country_name&#39;</span><span class="p">]</span></div>

<div class="viewcode-block" id="GeoIP.country"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.country">[docs]</a>    <span class="k">def</span> <span class="nf">country</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a dictionary with the country code and name when given an</span>
<span class="sd">        IP address or a Fully Qualified Domain Name (FQDN).  For example, both</span>
<span class="sd">        &#39;24.124.1.80&#39; and &#39;djangoproject.com&#39; are valid parameters.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Returning the country code and name</span>
        <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;country_code&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">country_code</span><span class="p">(</span><span class="n">query</span><span class="p">),</span>
                <span class="s1">&#39;country_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">country_name</span><span class="p">(</span><span class="n">query</span><span class="p">),</span>
                <span class="p">}</span></div>

    <span class="c1"># #### Coordinate retrieval routines ####</span>
<div class="viewcode-block" id="GeoIP.coords"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.coords">[docs]</a>    <span class="k">def</span> <span class="nf">coords</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">ordering</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;longitude&#39;</span><span class="p">,</span> <span class="s1">&#39;latitude&#39;</span><span class="p">)):</span>
        <span class="n">cdict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">city</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">cdict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">cdict</span><span class="p">[</span><span class="n">o</span><span class="p">]</span> <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">ordering</span><span class="p">)</span></div>

<div class="viewcode-block" id="GeoIP.lon_lat"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.lon_lat">[docs]</a>    <span class="k">def</span> <span class="nf">lon_lat</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="s2">&quot;Returns a tuple of the (longitude, latitude) for the given query.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">(</span><span class="n">query</span><span class="p">)</span></div>

<div class="viewcode-block" id="GeoIP.lat_lon"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.lat_lon">[docs]</a>    <span class="k">def</span> <span class="nf">lat_lon</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="s2">&quot;Returns a tuple of the (latitude, longitude) for the given query.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">coords</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;latitude&#39;</span><span class="p">,</span> <span class="s1">&#39;longitude&#39;</span><span class="p">))</span></div>

<div class="viewcode-block" id="GeoIP.geos"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.geos">[docs]</a>    <span class="k">def</span> <span class="nf">geos</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">query</span><span class="p">):</span>
        <span class="s2">&quot;Returns a GEOS Point object for the given query.&quot;</span>
        <span class="n">ll</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lon_lat</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ll</span><span class="p">:</span>
            <span class="kn">from</span> <span class="nn">django.contrib.gis.geos</span> <span class="k">import</span> <span class="n">Point</span>
            <span class="k">return</span> <span class="n">Point</span><span class="p">(</span><span class="n">ll</span><span class="p">,</span> <span class="n">srid</span><span class="o">=</span><span class="mi">4326</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div>

    <span class="c1"># #### GeoIP Database Information Routines ####</span>
    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">country_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns information about the GeoIP country database.&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">ci</span> <span class="o">=</span> <span class="s1">&#39;No GeoIP Country data in &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_country_file</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ci</span> <span class="o">=</span> <span class="n">GeoIP_database_info</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_country</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ci</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">city_info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns information about the GeoIP city database.&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">ci</span> <span class="o">=</span> <span class="s1">&#39;No GeoIP City data in &quot;</span><span class="si">%s</span><span class="s1">&quot;&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city_file</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">ci</span> <span class="o">=</span> <span class="n">GeoIP_database_info</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ci</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">info</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s2">&quot;Returns information about the GeoIP library and databases in use.&quot;</span>
        <span class="n">info</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">GeoIP_lib_version</span><span class="p">:</span>
            <span class="n">info</span> <span class="o">+=</span> <span class="s1">&#39;GeoIP Library:</span><span class="se">\n\t</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">GeoIP_lib_version</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">info</span> <span class="o">+</span> <span class="s1">&#39;Country:</span><span class="se">\n\t</span><span class="si">%s</span><span class="se">\n</span><span class="s1">City:</span><span class="se">\n\t</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">country_info</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">city_info</span><span class="p">)</span>

    <span class="c1"># #### Methods for compatibility w/the GeoIP-Python API. ####</span>
<div class="viewcode-block" id="GeoIP.open"><a class="viewcode-back" href="../../../../../ref/contrib/gis/geoip.html#django.contrib.gis.geoip.GeoIP.open">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">open</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">full_path</span><span class="p">,</span> <span class="n">cache</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">GeoIP</span><span class="p">(</span><span class="n">full_path</span><span class="p">,</span> <span class="n">cache</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_rec_by_arg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_city</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">city</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">country</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
    <span class="n">region_by_addr</span> <span class="o">=</span> <span class="n">city</span>
    <span class="n">region_by_name</span> <span class="o">=</span> <span class="n">city</span>
    <span class="n">record_by_addr</span> <span class="o">=</span> <span class="n">_rec_by_arg</span>
    <span class="n">record_by_name</span> <span class="o">=</span> <span class="n">_rec_by_arg</span>
    <span class="n">country_code_by_addr</span> <span class="o">=</span> <span class="n">country_code</span>
    <span class="n">country_code_by_name</span> <span class="o">=</span> <span class="n">country_code</span>
    <span class="n">country_name_by_addr</span> <span class="o">=</span> <span class="n">country_name</span>
    <span class="n">country_name_by_name</span> <span class="o">=</span> <span class="n">country_name</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>